<template>
	<scroll-view v-if="!loginFail" scroll-y="true" class="container">
		<view class="ling-view"></view>
		<view class="begin-assess" @click="actionClick">
			<assess-new-btn></assess-new-btn>
		</view>

		<view class="content">
			<view v-for="(item , index) in newList" class="item" @click="newClick(item)" :key="item.id">
				<image class="item-cover" :src="item.coverURL"></image>
				<view style="background-color: #F7F7F7;width: 1px;height: 100%;"></view>
				<view class="item-info">
					<view class="item-title">{{item.title}}</view>
					<view class="item-desc">{{item.subTitle}}</view>
				</view>
			</view>
		</view>
	</scroll-view>
	<view v-else class="login-view">
		<image src="../../static/about_bg_icon.png"></image>
		<text>登录失败</text>
		<button class="login-btn" @click="wxLogin" >
			点击重新登录
		</button>
	</view>
</template>

<script>
	import assessNewBtn from '../../components/assess-new-btn.vue'
	import utils from '../../utils/utils.js'
	export default {
		components: {
			assessNewBtn,
		},
		data() {
			return {
				loginFail: false,
				
				newList: [],
			}
		},
		
		onShareAppMessage(res) {
		    if (res.from === 'button') {// 来自页面内分享按钮
		      console.log(res.target)
		    }
			const appName = getApp().globalData.appName;
			return {
			  title: appName,
			  path: 'pages/index/index'
			}
		},

		onLoad() {
			uni.$on('event_get_user', this.refreshUserInfo);
			uni.$on('event_ticket_refresh', this.checkLogin)
		},
		
		onShow(){
			console.log('index onshow')
			this.checkLogin();
		},

		methods: {
			///检查会员信息 是否完成填写
			checkVipInfo() {
				console.log('checkVipInfo');

				this.vipLevel = getApp().globalData.user.org.vipLevel;
				if (utils.isEmpty(this.vipLevel)){
					this.vipLevel = 0;
				}
				let areaPhone = getApp().globalData.user.org.contactNumber;
				// this.vipLevel = 1;
				// areaPhone = '';
				let content = '建议您填写园所信息，相关信息会出现在测评报告上'
				if (getApp().globalData.appid != 'wx8033fe9de99c902d' && this.vipLevel === 0){
					return ;
				}
				if (getApp().globalData.appid === 'wx8033fe9de99c902d') {
					content = '请完善信息后，再开始测评哦！'
				}
				if (utils.isEmpty(areaPhone)){
					uni.showModal({
						title: '信息完善',
						content: content,
						showCancel: false,
						confirmColor: '#FACB2F',
						success: function(res) {
							uni.navigateTo({
								url:'/pages-assess/user-edit/user-edit'
							})
						}
					})
				}
			},
			
			checkLogin(){
				var ticket = ''
				try{
					ticket = uni.getStorageSync('token')
				}catch{}
				console.log('checkLogin:' + ticket)
				if (utils.isEmpty(ticket)){
					this.wxLogin();
				}else{
					// #ifdef MP
						this.checkVipInfo();
					// #endif
					this.wxCheckSession();
					this.refreshUserInfo();
					this.getNewList();
				}
			},
			
			wxCheckSession(){
				var that = this;
				// #ifdef MP-WEIXIN
					uni.checkSession({
						success() {
							//session_key 未过期，并且在本生命周期一直有效
							console.log('checkSession success');
						},
						fail() {
							// session_key 已经失效，需要重新执行登录流程
							console.log('checkSession fail');
							that.wxLogin();
						}
					})
				// #endif
				// #ifdef H5
					console.log('ifdef H5');
					try{
						uni.setStorageSync('token','52a6e407a1a7ef98a6b758f35e72723f-4-1637999803138');
						getApp().globalData.ticket = '52a6e407a1a7ef98a6b758f35e72723f-4-1637999803138';
					}catch(e){}
				// #endif	
			},
			
			wxLogin(){
				var that = this;
				// #ifdef MP-WEIXIN
					uni.showLoading({mask:true})
					uni.hideTabBar({})
					uni.login({
						provider: 'weixin',
						success: function(loginRes) {
							console.log(loginRes);
							that.assessLogin(loginRes.code,that);
						},
						fail: function(err){
							console.log(err);
							utils.showToast('微信登录失败')
							uni.hideLoading()
							that.loginFail = true;
						}
					});
				// #endif
				// #ifdef H5
					console.log('ifdef H5');
					try{
						uni.setStorageSync('token','52a6e407a1a7ef98a6b758f35e72723f-4-1637999803138');
						getApp().globalData.ticket = '52a6e407a1a7ef98a6b758f35e72723f-4-1637999803138';
					}catch(e){}
				// #endif
			},

			assessLogin(code,that) {
				var url = 'wx/user/' + getApp().globalData.appid + '/login';
				that.$http.request({
					url: url,
					data: {
						appid: getApp().globalData.appid,
						code: code,
					},
				}).then(res => {
					console.log(res);
					uni.hideLoading();
					if (res.code === "000000") {
						try {
							uni.setStorageSync('token', res.data.ticket);
							uni.setStorageSync('user',res.data.user);
							uni.setStorageSync('session',res.data.session);
						} catch (e) {}
						getApp().globalData.user = res.data.user;
						getApp().globalData.ticket = res.data.ticket;
						getApp().globalData.session = res.data.session;
						this.loginFail = false;
						uni.showTabBar({})
						this.getNewList();
						this.checkVipInfo();
					} else {
						this.loginFail = true;
						uni.showToast({
							title: '登录出错:'+res.msg,
							icon:'none'
						})
					}
				}).catch(err => {
					console.log(err);
					this.loginFail = false;
					utils.showToast(err);
					uni.hideLoading();
				});
			},
			
			///更新用户信息
			refreshUserInfo(){
				var that = this;
				this.$http.request({
					method:'GET',
					url:'/eapi/profile/vip_status'
				}).then(res => {
					console.log(res);
					if (res.code === "000000") {
						getApp().globalData.user = res.data;
						uni.$emit('event_refresh_user')
						try {
							uni.setStorageSync('user',res.data);
						} catch (e) {}
					}
				}).catch(err => {
					console.log(err);
				})
			},
			
			actionClick(){
				uni.navigateTo({
					url:'/pages-assess/assess/assess-ready/assess-ready?assess=0'
				})
			},

			newClick(item) {
				uni.navigateTo({
					url: '/pages-assess/web/web?url='+item.linkURL,
				})
			},
			
			getNewList() {
				this.$http.request({
					method:'GET',
					url:'/eapi/news/list',
					data: {
						pageNum: 0,
						pageSize: 10,
					},
				}).then(res => {
					console.log(res);
					if (res.code === "000000") {
						this.newList = res.data.content;
					}
				}).catch(err => {
					console.log(err);
				})
			}
		}
	}
</script>

<style lang="scss">	

	page{
		height: 100%;
	}

	.container {
		font-size: 14px;
		background-color: #F7F7F7;
		/* background-color: #4CD964; */
		display: flex;
		flex-direction: column;
		height: 100%;
	}

	.begin-assess {
		height: 160rpx;
		background-color: #FFFFFF;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.content {
		flex-grow: 1;
		padding-bottom: 20rpx;
	}

	.item {
		margin-left: 40rpx;
		margin-right: 40rpx;
		margin-top: 40rpx;

		height: 260rpx;
		background-color: #FFFFFF;
		border-radius: 8rpx;

		box-shadow: 0rpx 6rpx 12rpx rgba(0, 0, 0, 0.16);
		opacity: 1;

		display: flex;
		flex-direction: row;
		align-items: center;
	}

	.item-cover {
		width: 292rpx;
		height: 260rpx;
		border-top-left-radius: 8rpx;
		border-bottom-left-radius: 8rpx;
	}

	.item-info {
		height: 100%;
		flex: 1;
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-left: 14rpx;
		margin-right: 14rpx;
	}

	.item-title {
		font-size: 28rpx;
		color: #333333;
		font-weight: bold;
		margin-top: 20rpx;
	}

	.item-desc {
		font-size: 24rpx;
		color: #666666;
		margin-top: 10rpx;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 5;
		-webkit-box-orient:vertical;
		 flex-direction: column;
	}
	
	.login-view{
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		
		height: 100%;
	}
	
	.login-img{
		width: 267px;
		height: 269px;
	}
	
	.login-btn {
		width: 201px;
		height: 50px;
		background: linear-gradient(180deg, rgba(250, 204, 48, 1) 0%, rgba(252, 222, 122, 1) 100%);
		opacity: 1;
		border-radius: 4px;
		display: flex;
		justify-content: center;
		align-items: center;
		margin-top: 60rpx;
		color: #FFFFFF;
		font-size: 36rpx;
		font-weight: bold;
	}
</style>
